﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Windows.Forms;
using PlanningGIS.Framework;
using ESRI.ArcGIS.ADF.BaseClasses;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Controls;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.Geometry;
using PlanningGIS.Symbol;
using System.Collections;
using PlanningGIS.Win.Controls;
using PlanningGIS.ArcGIS;

namespace PlanningGIS.TocContextMenu.Button
{
    /// <summary>
    /// 作用：图层渲染
    /// </summary>
    internal class LayerRender : BaseCommand
    {
        /// <summary>
        /// 
        /// </summary>
        IApplication m_Application = null;

        /// <summary>
        /// 构造函数
        /// </summary>
        public LayerRender()
        {
            base.m_caption = "符号化";
            base.m_toolTip = base.Caption;
        }

        /// <summary>
        /// 创建
        /// </summary>
        /// <param name="hook"></param>
        public override void OnCreate(object hook)
        {
            if (m_Application == null)
            {
                m_Application = hook as IApplication;
            }
            
        }

        /// <summary>
        /// 单击事件
        /// </summary>
        public override void OnClick()
        {
            if (m_Application == null) return;
            if (string.IsNullOrWhiteSpace(AoHelper.StylePath)) AoHelper.StylePath = AppConfig.StylePath;
            ILayer lyr = (m_Application as IFramework).TOCControl.CustomProperty as ILayer;
            if (lyr is IFeatureLayer)
            {
                if (lyr is IGeoFeatureLayer)
                {
                    FrmLayerRender frm = new FrmLayerRender();
                    frm.FeatureLayer = (IFeatureLayer)lyr;
                    frm.ApplyChanged += delegate(object sender1, EventArgs e1)
                    {
                        if (frm.FeatureRenderer != null)
                        {
                            IGeoFeatureLayer geofeatlyr = (IGeoFeatureLayer)lyr;
                            geofeatlyr.Renderer = frm.FeatureRenderer;
                            (m_Application as IFramework).MapControl.Refresh(esriViewDrawPhase.esriViewGeography, null, null);
                            (m_Application as IFramework).TOCControl.Update();
                        }
                    };
                    frm.ShowDialog(FrameworkHelper.MdiForm);
                }
                else
                {
                    MsgHelper.ShowInfomationMsgbox("暂时不支持这种类型图层的符号化！");
                }
            }
            else if (lyr is IRasterLayer)
            {
                FrmRasterRender frm = new FrmRasterRender();
                frm.RasterLayer = lyr as IRasterLayer;
                frm.ApplyChangedEvent += delegate(object sender1, EventArgs e1)
                {
                    (m_Application as IFramework).MapControl.Refresh(esriViewDrawPhase.esriViewGeography, null, null);
                    (m_Application as IFramework).TOCControl.Update();
                };
                frm.ShowDialog(FrameworkHelper.MdiForm);
            }
        }

        /// <summary>
        /// 是否可用
        /// </summary>
        public override bool Enabled
        {
            get
            {
                if (m_Application == null) return false;
                return ((m_Application as IFramework).IsMapCtrlactive && (m_Application as IFramework).TOCControl.CustomProperty is ILayer) ? true : false;
            }
        }

        /// <summary>
        /// 图标
        /// </summary>
        public override int Bitmap
        {
            get
            {
                string bitmapResourceName = "PlanningGIS.TocContextMenu.Res." + this.GetType().Name + ".ico";
                System.IO.Stream stream = GetType().Assembly.GetManifestResourceStream(bitmapResourceName);
                if (stream == null) return 0;

                return new Bitmap(stream).GetHbitmap().ToInt32();
            }
        }
    }
}
